import app from "../main.js"

export function getXianlu(option) {
	console.log("获取线路",option);
	return new Promise((resolve, reject) => {
		app.qqmapsdk.direction({
			mode: "driving",
			from: option.from,
			to: option.to,
			success: (res) => {
				const coors = res.result.routes[0].polyline
				let pl = [] //路线集合
				const kr = 1000000; //坐标解压（返回的点串坐标，通过前向差分进行压缩）

				for (let i = 2; i < coors.length; i++) {
					coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
				}
				//将解压后的坐标放入点串数组pl中
				for (let i = 0; i < coors.length; i += 2) {
					pl.push({ latitude: coors[i], longitude: coors[i + 1] })
				}
				const resdata = {
					distance: res.result.routes[0].distance / 1000,
					points: pl
				}
				resolve(resdata)
			},
			fail: (err) => {
				console.log("获取线路失败",err);
				resolve({
					distance: 0,
					points: []
				})
			},
		})
	})

}


// 自动缩放地图

export function furthest(obj) {
	var EARTH_RADIUS = 6378137.0; //单位M
	var PI = Math.PI;
	// 转为角度
	function getRad(d) {
		return d * PI / 180.0;
	}

	let distance = {
		// 将地球按照圆形计算
		getGreatCircle: function(coordinate1, coordinate2) {
			var lat1 = coordinate1[0] // 纬度
			var lat2 = coordinate2[0]
			var lng1 = coordinate1[1] // 经度
			var lng2 = coordinate2[1]

			var radLat1 = getRad(lat1);
			var radLat2 = getRad(lat2);

			var a = radLat1 - radLat2;
			var b = getRad(lng1) - getRad(lng2);

			var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
			s = s * EARTH_RADIUS;
			s = Math.round(s * 10000) / 10000.0;

			return s;
		},
		// 将地球按照椭圆进行计算
		getFlattern: function(coordinate1, coordinate2) {
			var lat1 = coordinate1[0] // 纬度
			var lat2 = coordinate2[0]
			var lng1 = coordinate1[1] // 经度
			var lng2 = coordinate2[1]

			var f = getRad((lat1 + lat2) / 2);
			var g = getRad((lat1 - lat2) / 2);
			var l = getRad((lng1 - lng2) / 2);

			var sg = Math.sin(g);
			var sl = Math.sin(l);
			var sf = Math.sin(f);

			var s, c, w, r, d, h1, h2;
			var a = EARTH_RADIUS;
			var fl = 1 / 298.257;

			sg = sg * sg;
			sl = sl * sl;
			sf = sf * sf;

			s = sg * (1 - sl) + (1 - sf) * sl;
			c = (1 - sg) * (1 - sl) + sf * sl;

			w = Math.atan(Math.sqrt(s / c));
			r = Math.sqrt(s * c) / w;
			d = 2 * w * a;
			h1 = (3 * r - 1) / 2 / c;
			h2 = (3 * r + 1) / 2 / s;

			return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
		}

	}

	// obj包含中心点坐标和其他所有的坐标位置
	// 处理obj传入的参数
	let centralPoint = obj.centralPoint // 中心点
	let otherPoint = obj.otherPoint // 二维数组存放其他坐标点

	// 循环遍历得出距离最远的1个坐标然后*2
	let farthestCoordinate = [] // 当前最远坐标
	let farthestDistance = 0 // 最远距离
	let dis = null // 存储当前距离
	for (let i in otherPoint) {
		let newLocation = []
		newLocation[0] = otherPoint[i].latitude
		newLocation[1] = otherPoint[i].longitude

		dis = distance.getGreatCircle(centralPoint, newLocation)
		if (dis > farthestDistance) {
			farthestDistance = dis
			farthestCoordinate = newLocation
		}
	}

	farthestDistance = farthestDistance / 1000 // 转为km

	// 根据坐标计算出合理的比例尺寸
	let surface = [500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02]

	for (let i in surface) {
		if (farthestDistance > surface[i]) {
			let scale = 5 + Number(i)
			return scale
		}
	}

	return 16

}
